﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPMS.Model;

namespace GPMS.Web.Controllers
{
    using GPMS.Extension;
    public class GPSelectionController : BaseController
    {
        //
        // GET: /GPSelection/

        public ActionResult GPList()//学生课题列表
        {
            Dictionary<Guid, int> dic = new Dictionary<Guid, int>();
            Dictionary<Guid, string> guiders = new Dictionary<Guid, string>();
            Dictionary<Guid, bool> selected = new Dictionary<Guid, bool>();
            Guid UserID = GPMS.BLL.AccountHelper.GetLoginUserID(Request);
            if (gpmsDb.GPSelections.Any(g=>g.IsConfirm && g.UserID==UserID))
            {
                return Content("您已经有确定的毕业设计题目，不能再选题");
            }
            var list = gpmsDb.GraduationProjects.Where(g => g.Guider.Specialty.Users.Any(u => u.UserID == UserID) && g.Audit == true).ToList();//显示通过审核的相同专业的课题
            list.ForEach(i =>
            {
                dic.Add(i.GPID, gpmsDb.GPSelections.Count(g => g.GPID == i.GPID));
                guiders.Add(i.GPID, gpmsDb.GraduationProjects.First(g => g.GPID == i.GPID).Guider.RealName);
                selected.Add(i.GPID, gpmsDb.GPSelections.Any(g => g.GPID == i.GPID && g.UserID == UserID));
            });
            ViewData["dic"] = dic;
            ViewData["guider"] = guiders;
            ViewData["selected"] = selected;
            return View(list);
        }

        public ActionResult ProjectSelect(Guid id)
        {

            Guid userid = GPMS.BLL.AccountHelper.GetLoginUserID(Request);
            if (gpmsDb.GPSelections.Any(g => g.GPID == id && g.IsConfirm && g.UserID==userid))
            {
                return Error("您已经有确定的毕业设计题目");
            }
            return Confirm(string.Format("确定选择此毕业课题：{0}?", gpmsDb.GraduationProjects.FirstOrDefault(p => p.GPID == id).GPName));
        }
        [HttpPost]
        public ActionResult ProjectSelect(Guid id, FormCollection collection)//课题选择
        {
            if (gpmsDb.GraduationProjects.Where(g => g.GPID == id && g.GPSelections.Count(gps => gps.GPID == id) < g.Maxman).Count() < 1)
            {
                return Error("人满了。。。不能选了 ");

            }
            Guid userid = GPMS.BLL.AccountHelper.GetLoginUserID(Request);
           
            GPMS.Model.GPSelection model = new Model.GPSelection()
            {
                GPID = id,
                IsConfirm = false,
                SelectionID = Guid.NewGuid(),
                UserID = userid,
                SelectDate = DateTime.Now
            };
            
            gpmsDb.GPSelections.AddObject(model);
            gpmsDb.SaveChanges();
            return Success("选题成功");
        }

        public ActionResult UserList(Guid id )//当前课题拥有的用户列表
        {
            ViewData["gpid"] = id;
            return View(gpmsDb.GPSelections.Where(s => s.GPID == id)
                .Select(g => new GPMS.Model.GPSelectionVM()
                {
                    Project = g.GraduationProject,
                    Selector = g.User.UserName,
                    Selection = g
                }).ToList());
        }

        public ActionResult ConfirmSelection(Guid id)
        {
            return Confirm("确定选择此用户 ?");

        }
        [HttpPost]
        public ActionResult ConfirmSelection(Guid id,FormCollection collection)//老师确定学生选题
        {
            GPSelection selection = gpmsDb.GPSelections.FirstOrDefault(s => s.SelectionID== id);
            if (selection.IsConfirm != false)
            {
                return Error("用户已经通过选题");
            }
            selection.IsConfirm = true;

            UserInfo user = gpmsDb.GPSelections.FirstOrDefault(s => s.SelectionID == id).User;
            user.Status = UserStatusEnum.Designing.ToString();
            gpmsDb.GPSelections.Where(gps => gps.SelectionID != selection.SelectionID && gps.UserID == user.UserID).ToList().ForEach(i => gpmsDb.GPSelections.DeleteObject(i));
            gpmsDb.SaveChanges();
            return Success("成功");

        }

        public ActionResult Delete(Guid id)
        {
            return Confirm("确定删除此用户 ?");
        }
        [HttpPost]
        public ActionResult Delete(Guid id,FormCollection collection)//老师否定学生选题
        {
            GPSelection selection = gpmsDb.GPSelections.FirstOrDefault(s => s.SelectionID==id);
            gpmsDb.GPSelections.DeleteObject(selection);
            gpmsDb.SaveChanges();
             return Success("成功删除");
        }

        public ActionResult GPSelectionList()//导师的已被选课题列表
        {
            Guid id = GPMS.BLL.AccountHelper.GetLoginUserID(Request);
            var list = gpmsDb.GPSelections.Where(g=>g.GraduationProject.GuiderID == id).ToList();
            return View(list);
        }

        public ActionResult Rating(Guid id)
        {
            return View(gpmsDb.GPSelections.FirstOrDefault(gps=>gps.SelectionID==id));
        }

        [HttpPost]
        public ActionResult Rating(Guid id,FormCollection conllection)
        {
            GPSelection selection = gpmsDb.GPSelections.FirstOrDefault(g=>g.SelectionID == id);
            UpdateModel(selection,conllection);
            gpmsDb.SaveChanges();
            return Success("评分成功！！");
        }
    }
}
